<?php

  /**
   * Gestion de l'affichage de la liste paginée
   */

class Liste_Paginee_Ui extends Liste_Ui {

  protected $urlParam;
  protected $paginator;
  protected $blocPaginee;

  public function __construct($liste, $itemCountPerPage=10, $pageRange=9,$blocPaginee=false) {
    parent::__construct($liste);
	$this->blocPaginee = $blocPaginee; //dernier paramètre à true sil s'agit des bloc de listes paginées (ajax) -> les méthodes makeUrl et make Links se trouvent modifier
	$this->urlParam = array();
    $this->extraireQuery();
   // $this->urlParam = $this->extraireQuery();
    $this->paginator = new Zend_Paginator($liste);
    $page = isset($_GET['p']) ? $_GET['p'] : 1;
    $this->paginator->setCurrentPageNumber($page);
    $this->paginator->setItemCountPerPage($itemCountPerPage);
    $this->paginator->setPageRange($pageRange);
    $this->paginator->setDefaultScrollingStyle("Sliding");
  }

/*
  public function makeLinks(){
    $html = "";

    $pages = $this->paginator->getPages();
	var_dump($pages);
  //  $html .= "<div><a href=\"{$this->makeUrl($pages->previous)}\">< Precedent |</a>";

    foreach($pages->pagesInRange as $valeur){
      $url = $this->makeUrl($valeur);
      if($valeur == $pages->current) {
	$html .= " {$valeur} |";
      } else {
      	$html .= " <a href=\"{$url}\">{$valeur}</a> |";
      }
    }

    //$html .= "<a href=\"{$this->makeUrl($pages->next)}\"> Suivant></a></div>";

    return $html;
  }
*/

 protected function makeLinks() {
    if ($this->liste->count() == 0) {
      return "";
    }

    $pages = $this->paginator->getPages();
    if (sizeof($pages->pagesInRange) == 1) {
      return "";
    }
    
    $html = "<div class=\"links\">";
  


    // lien prÃ©cÃ©dent
    if (isset($pages->previous)) {
	  $html .= "<a class=\"blocP\" href=\"{$this->makeUrl($pages->first)}\">&lt;&lt;</a>"; // Premier
      $html .= "<a class=\"blocP\" href=\"{$this->makeUrl($pages->previous)}\">&lt;</a>";
    }

    // liens pages 
    foreach ($pages->pagesInRange as $page) {
      if ($page != $pages->current) {
    $html .= "<a class=\"blocP\" href=\"{$this->makeUrl($page)}\">{$page}</a>";
      } else {
    $html .= "<span class=\"current\">{$page}</span>";
      }
    } //endforeach

    //lien next
    if (isset($pages->next)) {
      $html .= "<a class=\"blocP\" href=\"{$this->makeUrl($pages->next)}\">&gt;</a>";
	  $html .= "<a class=\"blocP\" href=\"{$this->makeUrl($pages->last)}\">&gt;&gt;</a>"; // Dernier
    } 

    // derniÃ¨re 
    

    $html .= "</div>";

    return $html;
  }
  /*
  protected function extraireQuery(){
    $query = $_SERVER['QUERY_STRING'];
    $tab = explode("&", $query);
    $tabParam = array();
 	var_dump($tab);
    foreach($tab as $elem){
      $tab = explode("=", $elem);
      $tabParam[$tab['0']] = $tab['1'];
    }

    return $tabParam;
  }
  */
    protected function extraireQuery() {
    	$query = $_SERVER['QUERY_STRING'];    
    	$tab = explode("&", $query);
		if ($tab[0]!='') {
    		foreach ($tab as $param) {
     			$data = explode("=", $param);
     			if ($data[0] != "p" && $data[0] != "go") {
    				$this->urlParam[$data[0]] = $data[1];
      			}
    		}
		}
  }

  protected function makeUrl($numPage){
    $url = "index.php?";
	if ($this->blocPaginee){
		foreach($this->urlParam as $k=>$v){
			if($k != 'p' && $k != 'typeP' && $k!= 'blocTarget'){ // param blocTarget ajouté en javascript
			$url .= "{$k}={$v}&amp;";
			}
		}
		$url .= "typeP=bloc&amp;";
	} else {
		foreach($this->urlParam as $k=>$v){
			if($k != 'p' ){
			$url .= "{$k}={$v}&amp;";
			}
		}
	}
	$url .= "p={$numPage}";
	
    return $url;
  }

  public function makeHtml($classeUi, $methode, $coul=array(),$offset=0, $itemPerPage=""){

    $pages = $this->paginator->getPages();
    //$html = $this->makeLinks();
    $html = parent::makeHtml($classeUi, $methode, $coul, $pages->firstItemNumber-1, $pages->itemCountPerPage);
	
    $html .= "<div class=\"clearer\"></div>" . $this->makeLinks();

    return $html;
  }


  public function setPage($numPage){
    $this->paginator->setCurrentPageNumber($numPage);
  }







}
?>